/* ========================================================================
 * Copyright 2012 Katsushige Nakanishi (kat.nakanishi@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ========================================================================
 */

package com.android.deviceapi;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

import com.android.deviceapi.audio.AudioServlet;
import com.android.deviceapi.common.Constants;

public class HttpdServiceImpl extends Service implements HttpdService {

    private Server mServer;

    private LocalBinder mBinder = new LocalBinder();

    public class LocalBinder extends Binder {
        public HttpdService getService() {
            return HttpdServiceImpl.this;
        }
    }

    @Override
    public void onCreate() {

    }

    @Override
    public void onDestroy() {
        stopJettyServer();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        startJettyServer();
        // We want this service to continue running until it is explicitly
        // stopped, so return sticky.
        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent arg0) {
        return mBinder;
    }

    private void startJettyServer() {
        if (mServer == null) {
            mServer = new Server(8080);
            ServletContextHandler handler = new ServletContextHandler(
                    ServletContextHandler.SESSIONS);
            handler.setContextPath(DeviceApi.Path.ROOT);
            mServer.setHandler(handler);

            // servlets
            handler.addServlet(new ServletHolder(new AudioServlet(this)), DeviceApi.Path.AUDIO + "/*");

            try {
                mServer.start();
            } catch (Exception e) {
                Log.e(Constants.TAG, "failed to start Jetty server: " + e);
                // ignore
            }
        }
    }

    private void stopJettyServer() {
        if (mServer != null) {
            try {
                mServer.stop();
                mServer.join();
            } catch (Exception e) {
                Log.e(Constants.TAG, "failed to stop Jetty server: " + e);
                // ignore
            }
            mServer = null;
        }
    }

}
